use std::cell::RefCell;
use std::rc::Rc;
use crate::instruction::instruction::Instruction;
use crate::vm_thread::{VMFrame, VMThread};
use crate::vm_val::{VMValEnum, VMValInteger};

pub struct ishr {}

impl Instruction for ishr {
    fn exec(&mut self, frame: Rc<RefCell<VMFrame>>, thread: &VMThread) {
        let ro_frame = frame.borrow();
        let mut rw_operand_stack = ro_frame.operand_stack.borrow_mut();
        let v2 = rw_operand_stack.pop().unwrap();
        let v1 = rw_operand_stack.pop().unwrap();

        let i1 = VMValEnum::convert2integer(&v1).num;
        let i2 = VMValEnum::convert2integer(&v2).num & 0x1f;

        rw_operand_stack.push(VMValEnum::VMValInteger {
            val: VMValInteger {
                num: i1 >> i2
            }
        });
    }

    fn to_string(&self) -> String {
        String::from("ishr")
    }
}